﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Management;
using System.Globalization;
using System.Collections;
using Microsoft.Win32;
using MyExplorer;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace MyExplorer
{
    public partial class Form1 : Form
    {
        #region biến 

        string dicpath;
        string dicpathcpy;
        int index;

        int Index
        {
            get
            {
                return index;
            }
            set
            {
                index = value;
            }
        }

        TreeNode nodepath;

        string DicPath
        {
            get
            {
                return dicpath;
            }
            set
            {
                dicpath = value;
            }
        }

        string DicPathCpy
        {
            get
            {
                return dicpathcpy;
            }
            set
            {
                dicpathcpy = value;
            }
        }

        #endregion 


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TreeNode noderoot = new TreeNode();
            CreateTree(noderoot);
        }

        #region event Tree View

        private void tvFolder_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;

            if (node.SelectedImageIndex == 3)
            {
                if (node.Text == "CD Drive(F:)")
                {
                    DriveList(node);
                }
                else
                    MessageBox.Show("error");
            }
            else
                FileList(node);
        }

        private void tvFolder_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {

            TreeNode node = e.Node;
            node.Nodes.Clear();
            DriveList(node);
        }

        #endregion

        #region read file 
        void FileList(TreeNode nodecurrent)
        {
            if (nodecurrent.SelectedImageIndex == 0)
            {
                //nodecurrent.Tag = nodecurrent.Text;
                // string[] lvData = new string[4];
                //string[] stringFiles = Directory.GetFiles((string)nodecurrent.Tag);
            }
            if (nodecurrent.SelectedImageIndex != 0)
            {
                TreeNode tam = nodecurrent;
                nodepath = nodecurrent;
                string path = tam.Text;

                if (nodecurrent.SelectedImageIndex == 1 || nodecurrent.SelectedImageIndex == 3)//disk
                {
                    nodecurrent.Tag = TestLocation(tam);
                    DicPath = TestLocation(tam);
                    
                }
                else
                {
                    if (System.IO.Path.GetExtension(nodecurrent.Text.ToString()) == "")//folder
                    {
                        if (tam.Text == "java")
                        {
                            while (TestLocation(tam.Parent) == null)
                            {
                                path = tam.Parent.Text + "\\" + path;
                                tam = tam.Parent;
                            }
                            nodecurrent.Tag = TestLocation(tam.Parent) + path;
                            DicPath = TestLocation(tam.Parent) + path;
                        }
                        while (TestLocation(tam.Parent) == null)
                        {
                            path = tam.Parent.Text + "\\" + path;
                            tam = tam.Parent;
                        }
                        nodecurrent.Tag = TestLocation(tam.Parent) + path;
                        DicPath = TestLocation(tam.Parent) + path;
                    }
                }

                lvfolder.Items.Clear();

                string[] lvData = new string[5];

                string[] stringFiles = Directory.GetFiles((string)nodecurrent.Tag);
                string[] stringFolders = Directory.GetDirectories((string)nodecurrent.Tag);

                string stringFileName = "";
                string stringFolderName = "";
                DateTime dtCreateDate, dtModifyDate;
                Int64 lFileSize = 0;

                //folder
                foreach (string stringFolder in stringFolders)
                {
                    nodecurrent.Tag = stringFolder;
                    stringFolderName = GetPathName(stringFolder);

                    dtCreateDate = Directory.GetCreationTime((string)nodecurrent.Tag); //GetCreationTime(stringFileName);
                    dtModifyDate = Directory.GetLastWriteTime((string)nodecurrent.Tag); //GetLastWriteTime(stringFileName);

                    //tao listview data
                    lvData[0] = stringFolderName;

                    if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtCreateDate) == false)
                    {
                        lvData[2] = formatDate(dtCreateDate.AddHours(1));
                    }
                    else
                    {
                        lvData[2] = formatDate(dtCreateDate);
                    }

                    if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtModifyDate) == false)
                    {
                        lvData[3] = formatDate(dtModifyDate.AddHours(1));
                    }
                    else
                    {
                        lvData[3] = formatDate(dtModifyDate);
                    }

                    ListViewItem lvItem = new ListViewItem(lvData, 0);
                    lvfolder.Items.Add(lvItem);
                }
                //loop throught all files
                foreach (string stringFile in stringFiles)
                {
                    stringFileName = stringFile;

                    FileInfo objFileSize = new FileInfo(stringFileName);

                    lFileSize = objFileSize.Length;
                    dtCreateDate = objFileSize.CreationTime; //GetCreationTime(stringFileName);
                    dtModifyDate = objFileSize.LastWriteTime; //GetLastWriteTime(stringFileName);

                    lvData[0] = GetPathName(stringFileName);
                    lvData[1] = formatSize(lFileSize);

                    if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtCreateDate) == false)
                    {
                        lvData[2] = formatDate(dtCreateDate.AddHours(1));
                    }
                    else
                    {
                        lvData[2] = formatDate(dtCreateDate);
                    }

                    if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtModifyDate) == false)
                    {
                        lvData[3] = formatDate(dtModifyDate.AddHours(1));
                    }
                    else
                    {
                        lvData[3] = formatDate(dtModifyDate);
                    }

                    ListViewItem lvItem = new ListViewItem(lvData, 0);
                    //Icon ico = Icon.ExtractAssociatedIcon(lvData[0]);
                    lvfolder.Items.Add(lvItem);
                }
            }
        }
        void DriveList(TreeNode noderoot)
        {
            int imageIndex;
            int selectIndex;

            TreeNode node = null;

            if (noderoot.SelectedImageIndex == 0)
            {
                DriveInfo[] drives = DriveInfo.GetDrives();

                foreach (DriveInfo dri in drives)
                {

                    if (dri.DriveType == DriveType.Fixed)//local
                    {
                        imageIndex = 1;
                        selectIndex = 1;
                        node = new TreeNode(dri.ToString(), imageIndex, selectIndex);
                        if (dri.VolumeLabel != "")
                            node.Text = dri.VolumeLabel.ToString() + "(" + node.Text.Remove(2, 1) + ")";
                        else
                            node.Text = "Local Disk" + "(" + node.Text.Remove(2, 1) + ")";

                        noderoot.Nodes.Add(node);
                        node.Nodes.Add("");
                    }
                    if (dri.DriveType == DriveType.CDRom)
                    {
                        try
                        {

                            imageIndex = 3;
                            selectIndex = 3;
                            node = new TreeNode(dri.ToString(), imageIndex, selectIndex);
                            if (dri.IsReady)
                                node.Text = "CD Drive" + "(" + node.Text.Remove(2, 1) + ")";
                            else
                                node.Text = "Removable Disk" + "(" + node.Text.Remove(2, 1) + ")";
                            noderoot.Nodes.Add(node);
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            else
            {
                if (noderoot.SelectedImageIndex == 1 || noderoot.SelectedImageIndex == 3)// disk
                {
                    noderoot.Nodes.Clear();

                    noderoot.Tag = TestLocation(noderoot);

                    string[] stringDirectories = Directory.GetDirectories((string)noderoot.Tag);


                    foreach (string dir in stringDirectories)
                    {
                        TreeNode n = new TreeNode(dir, 5, 5);
                        n.Text = n.Text.Remove(0, 3);
                        try
                        {
                            if (Directory.GetDirectories(dir).Length != 0)
                            {
                                noderoot.Nodes.Add(n);
                                n.Nodes.Add("");
                            }
                            else
                            {
                                if (Directory.GetDirectories(dir).Length == 0)
                                    noderoot.Nodes.Add(n);
                            }
                        }
                        catch (Exception e)
                        {
                        }

                    }
                }

                if (noderoot.SelectedImageIndex == 5)// folder
                {
                    TreeNode tam = noderoot;
                    string path;
                    path = tam.Text;

                    while (TestLocation(tam.Parent) == null)
                    {
                        path = tam.Parent.Text + "\\" + path;
                        tam = tam.Parent;
                    }
                    noderoot.Tag = TestLocation(tam.Parent) + path;

                    noderoot.Nodes.Clear();

                    string[] stringDirectories = Directory.GetDirectories((string)noderoot.Tag);

                    foreach (string dir in stringDirectories)
                    {
                        TreeNode n = new TreeNode(dir, 5, 5);
                        try
                        {
                            if (Directory.GetDirectories(dir).Length != 0)
                            {
                                n.Text = GetPathName(n.Text);
                                noderoot.Nodes.Add(n);
                                n.Nodes.Add("");
                            }
                            else
                            {
                                n.Text = GetPathName(n.Text);
                                noderoot.Nodes.Add(n);
                            }
                        }
                        catch (Exception e)
                        {

                        }
                    }
                }
            }
        }

        #endregion 


        #region tool menu view
        private void tllist_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem b in viewToolStripMenuItem.DropDownItems)
                b.Checked = false;
            tllist.Checked = true;

            lvfolder.View = View.List;

        }

        private void tldetail_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem b in viewToolStripMenuItem.DropDownItems)
                b.Checked = false;
            tldetail.Checked = true;

            lvfolder.View = View.Details;
        }

        private void tlsmall_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem b in viewToolStripMenuItem.DropDownItems)
                b.Checked = false;

            lvfolder.View = View.SmallIcon;
        }

        private void tllarge_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem b in viewToolStripMenuItem.DropDownItems)
                b.Checked = false;

            lvfolder.View = View.LargeIcon;

        }

        private void tltile_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem b in viewToolStripMenuItem.DropDownItems)
                b.Checked = false;

            lvfolder.View = View.Tile;
        }

        #endregion

        #region event list view

        private int sortColumn = -1;

        private void lvfolder_ItemActivate(object sender, EventArgs e)
        {
            string type = System.IO.Path.GetExtension(lvfolder.Items[Index].Text.ToString());
            string path;

            
            //type == "" is folder, if not is file.
            if (type != "")//file
            {
                process myprocess = new process();
                if (DicPath == "D:\\" || DicPath == "C:\\" || DicPath == "E:\\")
                    path = DicPath + lvfolder.Items[Index].Text.ToString();
                else
                    path = DicPath + "\\" + lvfolder.Items[Index].Text.ToString();
                myprocess.Print(path);
            }
            else//folder
            {
                nodepath = nodepath.Nodes.Add(lvfolder.Items[Index].Text);
                FileList(nodepath);
            }
        }

        private void lvfolder_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (lvfolder.SelectedItems.Count > 0)
                {
                    lvfolder.ContextMenuStrip = contextMenuStrip2;
                }
            }
            else
                lvfolder.ContextMenuStrip = contextMenuStrip1;
        }

        private void lvfolder_MouseDown(object sender, MouseEventArgs e)
        {
            if (lvfolder.ContextMenuStrip == contextMenuStrip2)
                lvfolder.ContextMenuStrip = contextMenuStrip1;
        }


        private void lvfolder_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            // Determine whether the column is the same as the last column clicked.
            ItemComparer sorter = lvfolder.ListViewItemSorter as ItemComparer;
            if (sorter == null)
            {
                sorter = new ItemComparer(e.Column);
                sorter.Order = SortOrder.Ascending;
                lvfolder.ListViewItemSorter = sorter;
            }
            // if clicked column is already the column that is being sorted
            if (e.Column == sorter.Column)
            {
                // Reverse the current sort direction
                if (sorter.Order == SortOrder.Ascending)
                    sorter.Order = SortOrder.Descending;
                else
                    sorter.Order = SortOrder.Ascending;
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                sorter.Column = e.Column;
                sorter.Order = SortOrder.Ascending;
            }
            lvfolder.Sort();
        }

        public class ItemComparer : IComparer
        {
            //column used for comparison
            public int Column { get; set; }
            //Order of sorting
            public SortOrder Order { get; set; }
            public ItemComparer(int colIndex)
            {
                Column = colIndex;
                Order = SortOrder.None;
            }


            public int Compare(object a, object b)
            {
                int result;
                ListViewItem itemA = a as ListViewItem;
                ListViewItem itemB = b as ListViewItem;
                if (itemA == null && itemB == null)
                    result = 0;
                else if (itemA == null)
                    result = -1;
                else if (itemB == null)
                    result = 1;
                if (itemA == itemB)
                    result = 0;
                // datetime comparison
                DateTime x1, y1;
                // Parse the two objects passed as a parameter as a DateTime.
                if (!DateTime.TryParse(itemA.SubItems[Column].Text, out x1))
                    x1 = DateTime.MinValue;
                if (!DateTime.TryParse(itemB.SubItems[Column].Text, out y1))
                    y1 = DateTime.MinValue;
                result = DateTime.Compare(x1, y1);
                if (x1 != DateTime.MinValue && y1 != DateTime.MinValue)
                    goto done;
                //numeric comparison

                //alphabetic comparison
                result = String.Compare(itemA.SubItems[Column].Text, itemB.SubItems[Column].Text);

            done:
                // if sort order is descending.
                if (Order == SortOrder.Descending)
                    // Invert the value returned by Compare.
                    result *= -1;
                return result;
            }
        }

        private void lvfolder_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            Index = e.ItemIndex;
        }

        private void lvfolder_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                cmsdelete_Click(sender, e);
            }
        }
#endregion

        #region contextmenu
        private void cmsdetail_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem b in viewToolStripMenuItem.DropDownItems)
                b.Checked = false;
            foreach (ToolStripMenuItem b in cmsview.DropDownItems)
                b.Checked = false;

            cmsdetail.Checked = true;
            tldetail.Checked = true;

            lvfolder.View = View.Details;

        }

        private void cmslist_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem b in viewToolStripMenuItem.DropDownItems)
                b.Checked = false;
            foreach (ToolStripMenuItem b in cmsview.DropDownItems)
                b.Checked = false;

            cmslist.Checked = true;
            tllist.Checked = true;

            lvfolder.View = View.List;

        }

        private void cmssmall_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem b in viewToolStripMenuItem.DropDownItems)
                b.Checked = false;
            foreach (ToolStripMenuItem b in cmsview.DropDownItems)
                b.Checked = false;

            cmssmall.Checked = true;
            tlsmall.Checked = true;

            lvfolder.View = View.SmallIcon;

        }

        private void cmslarge_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem b in viewToolStripMenuItem.DropDownItems)
                b.Checked = false;
            foreach (ToolStripMenuItem b in cmsview.DropDownItems)
                b.Checked = false;

            cmslarge.Checked = true;
            tllarge.Checked = true;

            lvfolder.View = View.LargeIcon;
        }

        private void cmstile_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem b in viewToolStripMenuItem.DropDownItems)
                b.Checked = false;
            foreach (ToolStripMenuItem b in cmsview.DropDownItems)
                b.Checked = false;

            cmstile.Checked = true;
            tltile.Checked = true;

            lvfolder.View = View.Tile;
        }

        private void cmsname_Click(object sender, EventArgs e)
        {
        }

        private void cmssize_Click(object sender, EventArgs e)
        {

        }

        private void cmsrefesh_Click(object sender, EventArgs e)
        {
            lvfolder.Refresh();
        }

        private void cmscopy_Click(object sender, EventArgs e)
        {
            
            string fileName = lvfolder.Items[Index].Text;
            string sourcePath = DicPath;
            string targetPath = DicPath + fileName;

            DicPathCpy = targetPath;

            // Use Path class to manipulate file and directory paths.
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            //string destFile = System.IO.Path.Combine(targetPath, fileName);

            // To copy a folder's contents to a new location:
            // Create a new target folder, if necessary.
            //if (!System.IO.Directory.Exists(targetPath))
            //{
            //    System.IO.Directory.CreateDirectory(targetPath);
            //}

            // To copy a file to another location and 
            // overwrite the destination file if it already exists.
            //try
            //{
            //    System.IO.File.Copy(sourceFile, destFile, true);
            //}
            //catch (Exception ex)
            //{
            //}

            // To copy all the files in one directory to another directory.
            // Get the files in the source folder. (To recursively iterate through
            // all subfolders under the current directory, see
            // "How to: Iterate Through a Directory Tree.")
            // Note: Check for target path was performed previously
            //       in this code example.
            //if (System.IO.Directory.Exists(sourcePath))
            //{
            //    string[] files = System.IO.Directory.GetFiles(sourcePath);

            //    // Copy the files and overwrite destination files if they already exist.
            //    //foreach (string s in files)
            //    //{
            //    //    // Use static Path methods to extract only the file name from the path.
            //    //    fileName = System.IO.Path.GetFileName(s);
            //    //    destFile = System.IO.Path.Combine(targetPath, fileName);
            //    //    System.IO.File.Copy(s, destFile, true);
                //}
            //}
        }

        private void cmspaste_Click(object sender, EventArgs e)
        {
            string sourceFile = DicPathCpy;
            string destinationFile = DicPath + GetPathName(DicPathCpy);


            if (System.IO.Directory.Exists(sourceFile))
            {
                string[] files = System.IO.Directory.GetFiles(sourceFile);

                string fileName;
                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    System.IO.File.Copy(s, destinationFile, true);
                }
            }
            // To move a file or folder to a new location:
            //System.IO.File.Move(sourceFile, destinationFile);

            // To move an entire directory. To programmatically modify or combine
            // path strings, use the System.IO.Path class.
            //System.IO.Directory.Move(@"C:\Users\Public\public\test\", @"C:\Users\Public\private");
        }

        private void cmsdelete_Click(object sender, EventArgs e)
        {
            string path;

            path = DicPath + lvfolder.Items[Index].Text;

            DialogResult repense = MessageBox.Show("Do you want to delete " + lvfolder.Items[Index].Text, "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (repense == DialogResult.OK)
            {
                path = DicPath + lvfolder.Items[Index].Text;
                if (System.IO.Directory.Exists(path))
                {
                    try
                    {
                        System.IO.Directory.Delete(path);
                        lvfolder.Items.Remove(lvfolder.Items[Index]);
                    }
                    catch (System.IO.IOException ex)
                    {
                    }
                }
            }
        }

        private void cmsfolder_Click(object sender, EventArgs e)
        {
            string newpath;
            Folder fol = new Folder();
            newpath = fol.CreateFolder(DicPath);

            if (!System.IO.Directory.Exists(newpath))
            {
                System.IO.Directory.CreateDirectory(newpath);
                string[] lvData = new string[5];

                DateTime dtCreateDate, dtModifyDate;

                dtCreateDate = Directory.GetCreationTime(newpath); //GetCreationTime(stringFileName);
                dtModifyDate = Directory.GetLastWriteTime(newpath); //GetLastWriteTime(stringFileName);

                lvData[0] = GetPathName(newpath);

                if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtCreateDate) == false)
                {
                    lvData[2] = formatDate(dtCreateDate.AddHours(1));
                }
                else
                {
                    lvData[2] = formatDate(dtCreateDate);
                }

                if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtModifyDate) == false)
                {
                    lvData[3] = formatDate(dtModifyDate.AddHours(1));
                }
                else
                {
                    lvData[3] = formatDate(dtModifyDate);
                }

                ListViewItem lvItem = new ListViewItem(lvData, 0);
                lvfolder.Items.Add(lvItem);
            }
        }

        private void cmsrename_Click(object sender, EventArgs e)
        {
            string before = DicPath + lvfolder.Items[Index].Text;
            string tam = "thandeptrai";
            string after = Path.Combine(DicPath,  tam);

            Folder fol = new Folder();

            if (System.IO.Directory.Exists(after))
            {
                MessageBox.Show("error");
            }
            else
            {
                fol.RenameFolder(before, after);
                lvfolder.Items[Index].Text = tam;
            }
        }

        private void cmscut_Click(object sender, EventArgs e)
        {

        }

        private void cmsdocument_Click(object sender, EventArgs e)
        {
            string newpath;
            MyExplore.File_myex fil = new MyExplore.File_myex();
            newpath = fil.FileTxt(DicPath);

            if (!System.IO.Directory.Exists(newpath))
            {
                System.IO.Directory.CreateDirectory(newpath);
                string[] lvData = new string[5];

                DateTime dtCreateDate, dtModifyDate;

                dtCreateDate = Directory.GetCreationTime(newpath); //GetCreationTime(stringFileName);
                dtModifyDate = Directory.GetLastWriteTime(newpath); //GetLastWriteTime(stringFileName);

                lvData[0] = GetPathName(newpath);

                if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtCreateDate) == false)
                {
                    lvData[2] = formatDate(dtCreateDate.AddHours(1));
                }
                else
                {
                    lvData[2] = formatDate(dtCreateDate);
                }

                if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtModifyDate) == false)
                {
                    lvData[3] = formatDate(dtModifyDate.AddHours(1));
                }
                else
                {
                    lvData[3] = formatDate(dtModifyDate);
                }

                ListViewItem lvItem = new ListViewItem(lvData, 0);
                lvfolder.Items.Add(lvItem);

            }
            else
            {

            }

        }

        private void cmsopen_Click(object sender, EventArgs e)
        {
            string type = System.IO.Path.GetExtension(lvfolder.Items[Index].Text.ToString());
            string path;


            //type == "" is folder, if not is file.
            if (type != "")//file
            {
                process myprocess = new process();
                if (DicPath == "D:\\" || DicPath == "C:\\" || DicPath == "E:\\")
                    path = DicPath + lvfolder.Items[Index].Text.ToString();
                else
                    path = DicPath + "\\" + lvfolder.Items[Index].Text.ToString();
                myprocess.Print(path);
            }
            else//folder
            {
                nodepath = nodepath.Nodes.Add(lvfolder.Items[Index].Text);
                FileList(nodepath);
            }
        }

        #endregion

        #region other
        protected string GetPathName(string stringPath)
        {
            //Get Name of folder
            string[] stringSplit = stringPath.Split('\\');
            int _maxIndex = stringSplit.Length;
            return stringSplit[_maxIndex - 1];
        }

        protected string formatDate(DateTime dtDate)
        {
            string stringDate = "";

            stringDate = dtDate.ToShortDateString().ToString() + " " + dtDate.ToShortTimeString().ToString();

            return stringDate;
        }

        protected string formatSize(Int64 lSize)
        {
            //KB
            string stringSize = "";
            NumberFormatInfo myNfi = new NumberFormatInfo();

            Int64 lKBSize = 0;

            if (lSize < 1024)
            {
                if (lSize == 0)
                {
                    //zero byte
                    stringSize = "0";
                }
                else
                {
                    //nho hon 1 nhung khong phai la zero byte.
                    stringSize = "1";
                }
            }
            else
            {
                //chuyen sang KB
                lKBSize = lSize / 1024;

                stringSize = lKBSize.ToString("n", myNfi);
                //xoa so thap phan
                stringSize = stringSize.Replace(".00", "");
            }

            return stringSize + " KB";

        }


        string TestLocation(TreeNode node)
        {
            for (int i = 0; i < node.Text.Length; i++)
            {
                if (node.Text[i] == '(')
                {
                    if (node.Text[i + 1] == 'C')
                        return node.Text.Replace(node.Text, "C:\\");

                    if (node.Text[i + 1] == 'D')
                        return node.Text.Replace(node.Text, "D:\\");

                    if (node.Text[i + 1] == 'E')
                        return node.Text.Replace(node.Text, "E:\\");

                    if (node.Text[i + 1] == 'F')
                        return node.Text.Replace(node.Text, "F:\\");
                }
            }
            return null;
        }

        private void mnclose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult repense = MessageBox.Show("do you really want to close this app", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (repense == DialogResult.OK)
            {
                e.Cancel = false;
            }
            else
            {
                if (repense == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
        }

        void CreateTree(TreeNode noderoot)
        {
            tldetail.Checked = true;
            cmsdetail.Checked = true;

            noderoot = new TreeNode("MyComputer", 0, 0);
            tvFolder.Nodes.Add(noderoot);
            noderoot.Nodes.Add("");
        }

        #endregion

    }
}